<?php
########################################################################################
# Empresa :     Tecnopac Informatica;                                                  #
# Autor:        Deiverson Pacheco;                                                     #
# Data Criação: 10/12/2010;                                                            #
########################################################################################
session_start();
header("Cache-control: private");
include_once("../../includes/session.php");


//Linha para formatação de caracteres com acentuação
header("Content-Type: text/html; charset=ISO-8859-1",true);
extract($_REQUEST);
//======================================================================================
//==> Include obrigatórias no script
//======================================================================================
include_once('../../includes/classes/class.Conexao.php');
include_once('../../includes/classes/class.Geral.php');
include_once('../../includes/classes/class.Upload.php');


//======================================================================================
//==> Inicializar Classes
//======================================================================================
$not  = new Persist;
$not2 = new Persist;
$data = new getData;
$cor  = new ColorTr;
$util = new Util;
$vld  = new ValidarDados;
$sql  = new SqlInjection;

//======================================================================================
//==> Recebe variaveis de pesquisa
//======================================================================================
$id          = @$_GET['id'];
$evento      = @$_GET['evento'];

//======================================================================================
//==> Executa case conforme evento
//======================================================================================

if(isset($evento)){
	switch($evento){
		
				
			case 'adiciona_linha':
					$not2->sql = "
						
									SELECT * FROM produtos_descricao
								
						
						";
					if($not2->ler()>0){
						
						
						print
						 '<tr style="background-color:{cor};" onMouseOver="javascript:this.style.backgroundColor=#dce3e9"
							 onMouseOut="javascript:this.style.backgroundColor={cor}">';
						 
						print 
							'<td valign="middle" height="30">';
						
						print
							'<center>
								<input type="hidden" id="ck{cont}" name="ck{cont}" value="{par_id}" />
								<input type="checkbox" id="ckblinha'.$qtd.'" name="ckblinha'.$qtd.'" />
							</center>';
						print	
							'</td>
								<td width="8%" align="center" valign="middle" height="50">
									<select name="tipo_descricao'.$qtd.'" id="tipo_descricao'.$qtd.'" onchange=troca_descricao_valor(this.value,'.$qtd.') >
							';
						print "<option value=''> Selecione um Valor </option>";
							while($not2->ini())
									print "<option value='$not2->id_descricao'> $not2->ds_descricao </option>";
						print
							'
									</select> 
								</td>
							';
						print
							'<td width="80%" align="left" valign="middle">
								<font color="#000000" size="2" face="Arial, Helvetica, sans-serif">
									&nbsp; 
							';
									
						print '<select name="tipo_descricao_valor'.$qtd.'" id="tipo_descricao_valor'.$qtd.'" >';		
						print 	'<option value=""> Selecione um Valor </option>';
						print '</select>';
						
						print
							'
								</font>
							</td>
							</tr>
						';
					}
			exit;
		break;
		
		case 'adiciona_descricao_valor_select';
			$not2->sql = " SELECT * FROM produtos_descricao_valor
								WHERE id_descricao = $id_descricao ";
			
			$not2->ler();
			$retorno = '';
			while($not2->ini())
				$retorno .= $not2->id_descricao_valor.'_'.$not2->ds_valor.'|';
			
			print $retorno;
			exit;
		break;
		
		case 'altera':
			$not->sql = "DELETE FROM rel_produto_descricao WHERE id_produto = $id AND id_descricao = $id_desc";
			$not->del();
			
			foreach($_REQUEST as $value => $key){

				if(substr($value,0,20) == "tipo_descricao_valor"){
					$not->sql = " INSERT INTO rel_produto_descricao (id_produto,id_descricao,id_descricao_valor)
										VALUES ($id,$id_desc,$key) ";
					$not->ins();
				}
			}
			header("Location: popup_produtos_descricao.php?id=".$id);
		break;
		
		case 'novo':
			
			$not2->sql = "SELECT
							 MAX(rel_produto_descricao.id_descricao)+1 as id_nova_descricao
							 	FROM rel_produto_descricao
									WHERE id_produto  = $id";

			$not2->ler();
			$not2->ini();
			$id_nova_descricao = ($not2->id_nova_descricao != '' ? $not2->id_nova_descricao : 1);
			
			
			
			foreach($_REQUEST as $value => $key){

				if(substr($value,0,20) == "tipo_descricao_valor"){
					$not->sql = " INSERT INTO rel_produto_descricao (id_produto,id_descricao,id_descricao_valor)
										VALUES ($id,$id_nova_descricao,$key) ";
					$not->ins();
				}
			}
			header("Location: popup_produtos_descricao.php?id=".$id);
		break;
		
		
		case 'excluir': { 
				$id = $_GET['id'];
				$id_cad = $_GET['id_cad'];
				$codigos = explode(';',$id);
				
				for($i=1;count($codigos)>$i;$i++){ 
					
					
					$not->sql = "UPDATE rel_produto_descricao SET ativo = 0
									WHERE id_descricao = '$codigos[$i]'";
					$not->alt();
				}
				
				header("location: popup_produtos_descricao.php?id=$id_cad");

		break; } 	 	
		
	}
}

?>